package com.yunduo.world.handler.encoder;

import com.google.protobuf.ByteString;
import com.yunduo.world.entity.EntityInfo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledHeapByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.nio.ByteBuffer;
import java.util.List;

/**
 * 编码器 加上 \n
 */
public class EncoderPack extends MessageToMessageEncoder{

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List out) throws Exception {
        ByteBuf data = (ByteBuf) msg;
        byte[] originData = data.array();
        byte[] encode = new byte[originData.length+1];
        for (int i = 0; i < originData.length; i++) {
            encode[i] = originData[i];
        }
        encode[encode.length-1] = '\n';
        ByteBuf code = Unpooled.copiedBuffer(encode);
        out.add(code);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
    }
}
